//
//  XMMyAddressViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/29.
//

#import "XMMyAddressViewController.h"
#import "XMAreaSelectViewController.h"
#import "XMMyAddressView.h"
#import "XMNetwork+mine.h"
#import "XMHAreaModel.h"

@interface XMMyAddressViewController ()<XMMyAddressViewDelegate>

@property (nonatomic, strong) XMMyAddressView *addressView;

@end

@implementation XMMyAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.addressView];
    [self getMyAddressList];
}

//获取地址列表
- (void)getMyAddressList {
    XMWeakSelf;
    [_Network getMyAddressListSuccess:^(id  _Nonnull responseObject) {
        NSArray *addressModelArr = [NSArray yy_modelArrayWithClass:[XMAddressModel class] json:responseObject[@"data"]];
        [weakSelf.addressView setContentWithAddressModelArr:addressModelArr];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//创建地址
- (void)creatAddressWithAddressModel:(XMAddressModel *)addressModel {
    XMWeakSelf;
    [_Network creatAddressWithName:addressModel.name
                            mobile:addressModel.mobile
                            areaId:addressModel.areaId
                     detailAddress:addressModel.detailAddress
                     defaultStatus:(addressModel.defaultStatus ? @(1) : @(0))
                           success:^(id  _Nonnull responseObject) {
        [weakSelf.addressView addressAddSuccess];
        [weakSelf getMyAddressList];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//更新地址信息
- (void)addressEditWithAddressModel:(XMAddressModel *)addressModel {
    XMWeakSelf;
    [_Network addressUpdateWithId:addressModel.objectId
                             name:addressModel.name
                           mobile:addressModel.mobile
                           areaId:addressModel.areaId
                    detailAddress:addressModel.detailAddress
                    defaultStatus:addressModel.defaultStatus ? @(1) : @(0)
                          success:^(id  _Nonnull responseObject) {
        [weakSelf.addressView addressAddSuccess];
        [weakSelf getMyAddressList];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
//添加地址
- (void)addressAddWithAddressModel:(XMAddressModel *)addressModel {
    if (addressModel.isCreate) {
        [self creatAddressWithAddressModel:addressModel];
    }
    else {
        [self addressEditWithAddressModel:addressModel];
    }
}

- (void)areaSelect {
    //获取区域信息
    XMWeakSelf;
    [_Network getAreaListSuccess:^(id  _Nonnull responseObject) {
        NSArray *areaModelArr = [NSArray yy_modelArrayWithClass:[XMHAreaModel class] json:responseObject[@"data"]];
        XMAreaSelectViewController *areaSelectVC = [[XMAreaSelectViewController alloc] init];
        areaSelectVC.areaModelArr = areaModelArr;
        areaSelectVC.selectBlock = ^(NSString * _Nonnull areaStr, NSString * _Nonnull areaId) {
            if (areaStr.length > 0) {
                [weakSelf.addressView setContentWithAreaStr:areaStr areaId:areaId];
                NSLog(@"%@", areaId);
                NSLog(@"%@", areaStr);
//                weakSelf.userInfoModel.province = [areaStr componentsSeparatedByString:@"-"][0];
//                weakSelf.userInfoModel.city = [areaStr componentsSeparatedByString:@"-"][1];
            }
        };
        XMBaseNavigationController *areaNVC = [[XMBaseNavigationController alloc] initWithRootViewController:areaSelectVC];
        areaNVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [weakSelf presentViewController:areaNVC animated:YES completion:nil];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)addressSelectWithAddressModel:(XMAddressModel *)addressModel {
    self.addressModelSelectBlock ? self.addressModelSelectBlock(addressModel) : nil;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Init
- (XMMyAddressView *)addressView {
    if (!_addressView) {
        _addressView = [[XMMyAddressView alloc] initWithFrame:self.view.bounds];
        _addressView.delegate = self;
    }
    return _addressView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
